package com.sniper.util;

import java.text.DecimalFormat;
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Random;

/**
 * @author suzhen
 */
public class DataUtil {


    /**
     * 数据合并
     *
     * @param first
     * @param rest
     * @param <T>
     * @return
     */
    public static <T> T[] concat(T[] first, T[]... rest) {
        int totalLength = first.length;
        for (T[] array : rest) {
            totalLength += array.length;
        }
        T[] result = Arrays.copyOf(first, totalLength);
        int offset = first.length;
        for (T[] array : rest) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }
        return result;
    }

    public static String getLength(String str, int length) {
        StringBuilder sb = new StringBuilder();
        //不够9位0补齐
        for (int i = 0; i < length - str.length(); i++) {
            sb.append("0");
        }
        sb.append(str);
        return sb.toString();
    }

    /**
     * 获取一时间位准的流水号 供21位
     *
     * @return
     */
    public static String getSerialNumber() {
        Clock clock = Clock.system(ZoneId.of("Asia/Shanghai"));
        LocalDateTime localTime = LocalDateTime.now(clock);
        String timeCode = localTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
        Random random = new Random();
        double d = random.nextDouble();
        DecimalFormat decimalFormat = new DecimalFormat("#");
        d += 1;
        d *= 100;
        String dd = decimalFormat.format(d);
        return timeCode + getLength(dd, 4);
    }
}